<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理系统</title>
    <script src="./node_modules/axios/dist/axios.js"></script>

    <style>
        body {
            margin: 0;
            padding: 30px;
        }

        table,
        tr,
        th,
        td {
            border: 1px solid black;

            /* 边框合并 */
            border-spacing: 0;
            border-collapse: collapse;
        }

        td,
        th {
            padding: 5px, 10px;
        }

        a {
            text-decoration: none;
        }
    </style>
</head>

<body>
    <h1>用户列表</h1>
    <a href="./add.html">添加用户</a>

    <!-- br表示换行符 -->
    <br><br>

    <!-- 创建一个table列表 -->
    <table class="user-list">

        <tr>
            <th>id</th>
            <th>用户名</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
    </table>

    <script>
        axios.get('http://localhost:3000/users').then(res => {
            console.log(res.data)

            res.data.forEach(item => {
                // 获取Table对象
                const table = document.querySelector('.user-list')
                // 根据item中的数据动态创建tr对象
                const tr = document.createElement('tr')
                tr.innerHTML =  `<tr>
                                    <td>${item.id}</td>
                                    <td>${item.username}</td>
                                    <td>${item.password}</td>
                                    <td>
                                        <a href="./edit.html?id=${item.id}">编辑</a>
                                        <a href="./delete.html?id=${item.id}">删除</a>
                                    </td>
                                </tr>`

                // 将tr添加到table中
                table.appendChild(tr)
            });
        })

    </script>
</body>

</html>